package com.xcw.jac;

import org.java_websocket.client.WebSocketClient;
import org.java_websocket.handshake.ServerHandshake;

import java.net.URI;
import java.net.URISyntaxException;

public class WebSocketExample {

    public static void main(String[] args) {
        String url = "wss://stabilityai-stable-diffusion.hf.space/queue/join";

        try {
            WebSocketClient client = new WebSocketClient(new URI(url)) {
                @Override
                public void onOpen(ServerHandshake handshakedata) {
                    System.out.println("WebSocket opened");
                    // 在连接建立后，您可以在这里发送数据
                    String json = "{\n" +
                            "    \"session_hash\": \"12at5ubammgf\",\n" +
                            "    \"fn_index\": 3\n" +
                            "}";
                    send(json);
                    String json2 = "{\n" +
                            "    \"fn_index\": 3,\n" +
                            "    \"data\": [\n" +
                            "        \"dog\",\n" +
                            "        \"\",\n" +
                            "        9\n" +
                            "    ],\n" +
                            "    \"session_hash\": \"12at5ubammgf\"\n" +
                            "}";
                    send(json2);
                }

                @Override
                public void onMessage(String message) {
                    System.out.println("Received message: " + message);
                }

                @Override
                public void onClose(int code, String reason, boolean remote) {
                    System.out.println("WebSocket closed. Code: " + code + ", Reason: " + reason);
                }

                @Override
                public void onError(Exception ex) {
                    System.out.println("WebSocket error: " + ex.getMessage());
                }
            };

            client.connect();
        } catch (URISyntaxException e) {
            e.printStackTrace();
        }
    }
}